import { Config, getDefaultConfig } from './app_default.cfg'
import resolve from '@rollup/plugin-node-resolve'
import _ from 'lodash'
import path from 'path'
import { fileURLToPath } from 'url'
import { Plugin } from 'vite'

export function getBuildConfig(): Config {
  const config = {} as Config

  config.ENV = 'build'

  const clientDir = 'build/client'
  const serverDir = 'build/server'
  config.VITEX = {
    vite: {
      resolve: { alias: { '@': path.resolve(path.dirname(fileURLToPath(import.meta.url)), 'src') } },
    },
    viteServer: {
      build: {
        outDir: serverDir,
        rollupOptions: {
          plugins: [ resolve() as Plugin ],
          input: [],
          output: {
            format: 'cjs',
            chunkFileNames: '[name].cjs',
            assetFileNames: 'assets/[name][extname]'
          }
        }
      }
    },
    viteClient: {
      build: {
        outDir: clientDir,
        reportCompressedSize: false,
        sourcemap: false,
        rollupOptions: {
          plugins: [ resolve() as Plugin ],
          input: [],
          output: {
            format: 'esm',
            chunkFileNames: '[name].js',
            assetFileNames: 'assets/[name][extname]'
          }
        }
      }
    }
  }

  config.MONGODB = {
    ignore: true,
    uri: ''
  }

  // config.REDIS = {
  //   ignore: true,
  //   port: 0,
  //   host: '',
  //   password: '',
  //   db: 0
  // }

  // config.INFLUX = {
  //   ignore: true,
  //   options: { url: '' },
  //   org: ''
  // }

  return _.merge(getDefaultConfig(), config)
}